package com.sinaapp.gavinzhang.GSpider.plugin.redis;

import com.sinaapp.gavinzhang.GSpider.core.AbstractGWebUrlList;
import redis.clients.jedis.Jedis;

/**
 * 使用redis数据库实现的已访问列表
 * Created by gavin on 15-9-10.
 */
public class RedisFoundWebUrlList extends AbstractGWebUrlList {
    private Jedis jedis;

    public RedisFoundWebUrlList(Jedis jedis) {
        this.jedis = jedis;
    }

    @Override
    public boolean exist(String webUrl) {
        return jedis.sismember("foundUrlList",webUrl);
    }

    @Override
    public String get() {
        return null;
    }

    @Override
    public AbstractGWebUrlList add(String webUrl) {
        jedis.sadd("foundUrlList",webUrl);
        return this;
    }

    @Override
    public AbstractGWebUrlList remove(String webUrl) {
        jedis.srem("foundUrlList", webUrl);
        return this;
    }

    @Override
    public int size() {
        return jedis.scard("foundUrlList").intValue();
    }

    /*public static void main(String[] args)
    {
        Jedis jedis = new Jedis("localhost");
        RedisFoundWebUrlList redisFoundWebUrlList = new RedisFoundWebUrlList(jedis);
        System.out.println(redisFoundWebUrlList.add("www.baidu.com"));
        System.out.println(redisFoundWebUrlList.size());
        System.out.println(redisFoundWebUrlList.exist("www.baidu.com"));
        System.out.println(redisFoundWebUrlList.size());
        //System.out.println(redisFoundWebUrlList.remove("www.baidu.com"));
        System.out.println(redisFoundWebUrlList.size());
        //System.out.println(redisFoundWebUrlList.exist("www.baidu.com"));
        System.out.println(redisFoundWebUrlList.size());
    }*/
}
